<template>
  <div>
    <!-- 定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>学生管理</el-breadcrumb-item>
      <el-breadcrumb-item>基本信息</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 定义卡片 -->
    <el-card class="box-card">

      <!-- 定义第一行数据 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryItemInfo.query" clearable @clear="getStudentList">
            <el-button slot="append" icon="el-icon-search" @click="getStudentList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="toAddItem">添加学生信息</el-button>
        </el-col>
      </el-row>

      <!-- 获取商品列表数据 -->
      <el-table :data="studentList" style="width: 100%" stripe border>
        <el-table-column type="index" label="序号" width="50px">
        </el-table-column>
        <el-table-column prop="name" label="姓名" width="100px">
        </el-table-column>
        <el-table-column prop="account" label="学号" width="140px">
        </el-table-column>
        <!-- 使用过滤器修改时间 在main.js中定义过滤器 -->
 <!--       <template slot-scope="scope">
          {{scope.row.price | priceFormat}}
        </template> -->
        <el-table-column prop="sex" label="性别" width="50px">
        </el-table-column>
        <el-table-column prop="addr" label="籍贯" width="140px">
        </el-table-column>
        <el-table-column prop="age" label="年龄" width="50px">
        </el-table-column>
        <el-table-column prop="grade.gradeName" label="院系" width="150px">
        </el-table-column>
        <el-table-column prop="grade.className" label="班级" width="180px">
        </el-table-column>
        <el-table-column label="操作" >
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="updateStudentBtn(scope.row)">修改</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteStudentBtn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>


      <!-- 定义分页插件-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryItemInfo.pageNum" :page-sizes="[1, 5, 10, 20]" :page-size="queryItemInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>

    </el-card>
    <el-dialog title="商品修改" :visible.sync="updateStudentialog" width="50%">
      <el-form :model="updateStudent" :rules="rules" ref="updateItem" label-width="100px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="updateStudent.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="学号" prop="account">
          <el-input v-model="updateStudent.account" disabled></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-input v-model="updateStudent.sex"></el-input>
        </el-form-item>
        <el-form-item label="籍贯" prop="addr">
          <el-input v-model="updateStudent.addr"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="updateStudent.age"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateStudentialog = false">取 消</el-button>
        <el-button type="primary" @click="updateStudentForm">确 定</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<script>
  export default {
    data() {
      return {
        updateStudentialog: false,
        //1.定义商品列表信息
        studentList: [],
        //2.定义分页对象
        queryItemInfo: {
          query: '', //定义查询参数
          pageNum: 1,
          pageSize: 10
        },
        total: 0,
        updateStudent: {}
      }
    },
    created() {
      //1.获取商品列表数据
      this.getStudentList()
    },
    methods: {
      //实现商品信息分页查询
      async getStudentList() {
        const {
          data: result
        } = await this.$http.get("/student/getStudentList", {
          params: this.queryItemInfo
        })
        if (result.status !== 200) return this.$message.error("商品列表查询失败")
        this.studentList = result.data.rows
        this.total = result.data.total
      },
      //通过JS格式化时间
      formatDate(row, column, cellValue, index) {
        let date = new Date(cellValue)
        let year = date.getFullYear()
        let month = (date.getMonth() + 1 + '').padStart(2, '0')
        let day = (date.getDate() + '').padStart(2, '0')
        return year + '-' + month + '-' + day
      },
      //条数变化时 调用
      handleSizeChange(size) {
        this.queryItemInfo.pageSize = size
        this.getStudentList()
      },
      //页码值变化时 调用
      handleCurrentChange(current) {
        this.queryItemInfo.pageNum = current
        this.getStudentList()
      },
      async updateStatus(student) {
        const {
          data: result
        } = await this.$http.put("/item/updateItemStatus", {
          id: item.id,
          status: item.status
        })
        if (result.status !== 200) return this.$message.error("更新状态失败")
        this.$message.success("更新状态成功")
      },
      async deleteStudentBtn(student) {
        //消息确认框
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          //根据id删除数据
          const {
            data: result
          } = await this.$http.delete("/student/deleteStudentById", {
            params: {
              id: student.id
            }
          })
          if (result.status !== 200) return this.$message.error("商品删除失败")
          this.$message.success("商品删除成功")
          //重新获取商品列表信息
          this.getStudentList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      //转向到商品新增页面
      toAddItem(){
        this.$router.push("/item/addItem")
      },
      updateStudentBtn(student){
        this.updateStudent = student
        this.updateStudentialog = true
      },
      async updateStudentForm() {
        const {data : result} = await this.$http.put('/student/updateStudentById',this.updateStudent)
        if(result.status !== 200 ) return this.$message.error("修改信息失败！")
        this.$message.success("修改信息成功！")
        this.getStudentList()
        this.updateStudentialog = false
      }
    }
  }
</script>
<!-- 防止组件样式冲突 -->
<style lang="less" scoped>

</style>
